﻿using CoreShop.Model;
using Newtonsoft.Json.Linq;
using Sunny.UI;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms.VisualStyles;

namespace CoreShop.PrintService
{
    public class UIEditor : UIPanel
    {
        private UITextBox edtWeight;
        private UILabel uiLabel11;
        private UIComboBox edtAlign;
        private UITextBox edtDataPropertyName;
        private UITextBox edtText;
        private UILabel uiLabel3;
        private UILabel uiLabel4;
        private UILabel uiLabel1;
        private UIComboBox dataAlign;
        private UILabel uiLabel5;
        private UIComboBox cbmPrintField;
        private MenuItem item;        

        public VerticalAlignment VerAlign { get; set; }

        public int Column { get; set; }

        public void Init(int billtypeId)
        {
            dataAlign.SelectedIndex = 1;
            edtAlign.SelectedIndex = 0;

            var mMenuObj = new MenuObject();
            this.cbmPrintField.Items.Clear();
            var printList = new List<ComboxListDto>();
            var menuObjectNull = new ComboxListDto();
            menuObjectNull.name = "";
            menuObjectNull.value = "";
            printList.Add(menuObjectNull);

            var printList1 = Library.ModelHelper.GetCustomAttribute<MenuObject>(mMenuObj);
            printList.AddRange(printList1);

            this.cbmPrintField.Enabled = false;
            this.cbmPrintField.DataSource = printList;
            this.cbmPrintField.DisplayMember = "name";
            this.cbmPrintField.ValueMember = "value";
            if (printList.Count > 0) this.cbmPrintField.SelectedValue = "";
            this.cbmPrintField.Enabled = true;
        }

        public bool Check(bool center)
        {
            bool isOk;
            if (center)
                isOk = edtText.Text.IsValid() && edtDataPropertyName.Text.IsValid();
            else
                isOk = edtText.Text.IsValid() || edtDataPropertyName.Text.IsValid();

            if (!isOk)
            {
                if (edtText.Text.IsNullOrEmpty()) UIMessageTip.ShowWarning(edtText, "文本不能为空");
                if (center && edtDataPropertyName.Text.IsNullOrEmpty()) UIMessageTip.ShowWarning(edtDataPropertyName, "数据不能为空");
            }

            return isOk;
        }

        public void SetItem(MenuItem value)
        {
            if (value == null) return;
            item = value;

            edtText.Text = value.Text;
            edtAlign.SelectedIndex = (int)item.TextAlign;
            edtDataPropertyName.Text = value.DataPropertyName;
            edtWeight.IntValue = value.Weight;
            edtText.Text = value.Text;
            dataAlign.SelectedIndex = -1;
            dataAlign.SelectedIndex = (int)item.DataAlign;
            cbmPrintField.Enabled = false;
            cbmPrintField.SelectedValue = value.DataPropertyName;
            cbmPrintField.Enabled = true;
        }
               

        public MenuItem GetItem()
        {
            if (edtAlign.SelectedIndex < 0) edtAlign.SelectedIndex = 0;
            if (dataAlign.SelectedIndex < 0) dataAlign.SelectedIndex = 0;
            if (item == null) item = new MenuItem();

            item.Text = edtText.Text;
            item.TextAlign = (eTextAlignMode)edtAlign.SelectedIndex;
            item.DataPropertyName = edtDataPropertyName.Text;
            item.Weight = edtWeight.IntValue;
            item.DataAlign = (eTextAlignMode)dataAlign.SelectedIndex;

            if (VerAlign != VerticalAlignment.Center && item.Text.IsValid())
            {
                if (item.TextAlign == eTextAlignMode.Middle)
                {
                    if (item.DataPropertyName.IsValid())
                        item.DataAlign = eTextAlignMode.Middle;
                }

                if (item.TextAlign == eTextAlignMode.Right)
                {
                    if (item.DataPropertyName.IsValid())
                        item.DataAlign = eTextAlignMode.Right;
                }
            }

            return item;
        }

        [DefaultValue(8)]
        public int Weight
        {
            get => edtWeight.IntValue;
            set => edtWeight.IntValue = value;
        }

        public UIEditor()
        {
            InitializeComponent();
            ShowText = false;
        }

        private void InitializeComponent()
        {
            this.edtWeight = new Sunny.UI.UITextBox();
            this.uiLabel11 = new Sunny.UI.UILabel();
            this.edtAlign = new Sunny.UI.UIComboBox();
            this.edtDataPropertyName = new Sunny.UI.UITextBox();
            this.edtText = new Sunny.UI.UITextBox();
            this.uiLabel3 = new Sunny.UI.UILabel();
            this.uiLabel4 = new Sunny.UI.UILabel();
            this.uiLabel1 = new Sunny.UI.UILabel();
            this.dataAlign = new Sunny.UI.UIComboBox();
            this.uiLabel5 = new Sunny.UI.UILabel();
            this.cbmPrintField = new Sunny.UI.UIComboBox();
            this.SuspendLayout();
            // 
            // edtWeight
            // 
            this.edtWeight.ButtonSymbolOffset = new System.Drawing.Point(0, 0);
            this.edtWeight.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.edtWeight.DoubleValue = 8D;
            this.edtWeight.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.edtWeight.IntValue = 8;
            this.edtWeight.Location = new System.Drawing.Point(76, 5);
            this.edtWeight.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.edtWeight.Maximum = 100D;
            this.edtWeight.Minimum = 1D;
            this.edtWeight.MinimumSize = new System.Drawing.Size(1, 1);
            this.edtWeight.Name = "edtWeight";
            this.edtWeight.Padding = new System.Windows.Forms.Padding(5);
            this.edtWeight.ShowText = false;
            this.edtWeight.Size = new System.Drawing.Size(62, 26);
            this.edtWeight.TabIndex = 0;
            this.edtWeight.Text = "8";
            this.edtWeight.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            this.edtWeight.Type = Sunny.UI.UITextBox.UIEditType.Integer;
            this.edtWeight.Watermark = "";
            // 
            // uiLabel11
            // 
            this.uiLabel11.AutoSize = true;
            this.uiLabel11.BackColor = System.Drawing.Color.Transparent;
            this.uiLabel11.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiLabel11.Location = new System.Drawing.Point(6, 8);
            this.uiLabel11.Name = "uiLabel11";
            this.uiLabel11.Size = new System.Drawing.Size(65, 20);
            this.uiLabel11.TabIndex = 35;
            this.uiLabel11.Text = "字符个数";
            this.uiLabel11.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // edtAlign
            // 
            this.edtAlign.DataSource = null;
            this.edtAlign.DropDownStyle = Sunny.UI.UIDropDownStyle.DropDownList;
            this.edtAlign.FillColor = System.Drawing.Color.White;
            this.edtAlign.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.edtAlign.Items.AddRange(new object[] {
            "靠左",
            "居中",
            "靠右"});
            this.edtAlign.Location = new System.Drawing.Point(342, 5);
            this.edtAlign.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.edtAlign.MinimumSize = new System.Drawing.Size(63, 0);
            this.edtAlign.Name = "edtAlign";
            this.edtAlign.Padding = new System.Windows.Forms.Padding(0, 0, 30, 2);
            this.edtAlign.Size = new System.Drawing.Size(63, 26);
            this.edtAlign.TabIndex = 2;
            this.edtAlign.Text = "靠左";
            this.edtAlign.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            this.edtAlign.Watermark = "";
            // 
            // edtDataPropertyName
            // 
            this.edtDataPropertyName.ButtonSymbolOffset = new System.Drawing.Point(0, 0);
            this.edtDataPropertyName.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.edtDataPropertyName.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.edtDataPropertyName.Location = new System.Drawing.Point(479, -2);
            this.edtDataPropertyName.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.edtDataPropertyName.MinimumSize = new System.Drawing.Size(1, 1);
            this.edtDataPropertyName.Name = "edtDataPropertyName";
            this.edtDataPropertyName.Padding = new System.Windows.Forms.Padding(5);
            this.edtDataPropertyName.ShowText = false;
            this.edtDataPropertyName.Size = new System.Drawing.Size(57, 26);
            this.edtDataPropertyName.TabIndex = 3;
            this.edtDataPropertyName.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            this.edtDataPropertyName.Visible = false;
            this.edtDataPropertyName.Watermark = "";
            // 
            // edtText
            // 
            this.edtText.ButtonSymbolOffset = new System.Drawing.Point(0, 0);
            this.edtText.Cursor = System.Windows.Forms.Cursors.IBeam;
            this.edtText.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.edtText.Location = new System.Drawing.Point(185, 5);
            this.edtText.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.edtText.MinimumSize = new System.Drawing.Size(1, 1);
            this.edtText.Name = "edtText";
            this.edtText.Padding = new System.Windows.Forms.Padding(5);
            this.edtText.ShowText = false;
            this.edtText.Size = new System.Drawing.Size(110, 26);
            this.edtText.TabIndex = 1;
            this.edtText.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            this.edtText.Watermark = "";
            // 
            // uiLabel3
            // 
            this.uiLabel3.AutoSize = true;
            this.uiLabel3.BackColor = System.Drawing.Color.Transparent;
            this.uiLabel3.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiLabel3.Location = new System.Drawing.Point(300, 8);
            this.uiLabel3.Name = "uiLabel3";
            this.uiLabel3.Size = new System.Drawing.Size(37, 20);
            this.uiLabel3.TabIndex = 30;
            this.uiLabel3.Text = "位置";
            this.uiLabel3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // uiLabel4
            // 
            this.uiLabel4.AutoSize = true;
            this.uiLabel4.BackColor = System.Drawing.Color.Transparent;
            this.uiLabel4.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiLabel4.Location = new System.Drawing.Point(410, 8);
            this.uiLabel4.Name = "uiLabel4";
            this.uiLabel4.Size = new System.Drawing.Size(37, 20);
            this.uiLabel4.TabIndex = 29;
            this.uiLabel4.Text = "数据";
            this.uiLabel4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // uiLabel1
            // 
            this.uiLabel1.AutoSize = true;
            this.uiLabel1.BackColor = System.Drawing.Color.Transparent;
            this.uiLabel1.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiLabel1.Location = new System.Drawing.Point(143, 8);
            this.uiLabel1.Name = "uiLabel1";
            this.uiLabel1.Size = new System.Drawing.Size(37, 20);
            this.uiLabel1.TabIndex = 27;
            this.uiLabel1.Text = "标题";
            this.uiLabel1.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // dataAlign
            // 
            this.dataAlign.DataSource = null;
            this.dataAlign.DropDownStyle = Sunny.UI.UIDropDownStyle.DropDownList;
            this.dataAlign.FillColor = System.Drawing.Color.White;
            this.dataAlign.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.dataAlign.Items.AddRange(new object[] {
            "靠左",
            "居中",
            "靠右"});
            this.dataAlign.Location = new System.Drawing.Point(609, 5);
            this.dataAlign.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.dataAlign.MinimumSize = new System.Drawing.Size(63, 0);
            this.dataAlign.Name = "dataAlign";
            this.dataAlign.Padding = new System.Windows.Forms.Padding(0, 0, 30, 2);
            this.dataAlign.Size = new System.Drawing.Size(63, 26);
            this.dataAlign.TabIndex = 4;
            this.dataAlign.Text = "靠左";
            this.dataAlign.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            this.dataAlign.Watermark = "";
            // 
            // uiLabel5
            // 
            this.uiLabel5.AutoSize = true;
            this.uiLabel5.BackColor = System.Drawing.Color.Transparent;
            this.uiLabel5.Font = new System.Drawing.Font("微软雅黑", 10.5F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.uiLabel5.Location = new System.Drawing.Point(567, 8);
            this.uiLabel5.Name = "uiLabel5";
            this.uiLabel5.Size = new System.Drawing.Size(37, 20);
            this.uiLabel5.TabIndex = 37;
            this.uiLabel5.Text = "位置";
            this.uiLabel5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
            // 
            // cbmPrintField
            // 
            this.cbmPrintField.DataSource = null;
            this.cbmPrintField.DropDownStyle = Sunny.UI.UIDropDownStyle.DropDownList;
            this.cbmPrintField.FillColor = System.Drawing.Color.White;
            this.cbmPrintField.Font = new System.Drawing.Font("宋体", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.cbmPrintField.FormattingEnabled = true;
            this.cbmPrintField.Location = new System.Drawing.Point(452, 5);
            this.cbmPrintField.Margin = new System.Windows.Forms.Padding(4, 5, 4, 5);
            this.cbmPrintField.MinimumSize = new System.Drawing.Size(63, 0);
            this.cbmPrintField.Name = "cbmPrintField";
            this.cbmPrintField.Padding = new System.Windows.Forms.Padding(0, 0, 30, 2);
            this.cbmPrintField.Size = new System.Drawing.Size(110, 26);
            this.cbmPrintField.TabIndex = 226;
            this.cbmPrintField.TextAlignment = System.Drawing.ContentAlignment.MiddleLeft;
            this.cbmPrintField.Watermark = "";
            this.cbmPrintField.SelectedIndexChanged += new System.EventHandler(this.cbmPrintField_SelectedIndexChanged);
            // 
            // UIEditor
            // 
            this.Controls.Add(this.edtDataPropertyName);
            this.Controls.Add(this.cbmPrintField);
            this.Controls.Add(this.dataAlign);
            this.Controls.Add(this.uiLabel5);
            this.Controls.Add(this.edtWeight);
            this.Controls.Add(this.uiLabel11);
            this.Controls.Add(this.edtAlign);
            this.Controls.Add(this.uiLabel3);
            this.Controls.Add(this.uiLabel4);
            this.Controls.Add(this.uiLabel1);
            this.Controls.Add(this.edtText);
            this.Font = new System.Drawing.Font("微软雅黑", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
            this.Name = "UIEditor";
            this.RectSides = System.Windows.Forms.ToolStripStatusLabelBorderSides.None;
            this.Size = new System.Drawing.Size(680, 37);
            this.ResumeLayout(false);
            this.PerformLayout();

        }

        private void cbmPrintField_SelectedIndexChanged(object sender, System.EventArgs e)
        {
            if (cbmPrintField.Enabled)  edtDataPropertyName.Text = cbmPrintField.SelectedValue.ToString();
        }
    }
}
